﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using pswb.Classes;

namespace pswb.Forms.Wizard
{
    public partial class frmConnectonWizard : Form
    {
        #region Properties
        public string ServerType { get; set; }
        public string ConnectionString { get; set; }
        #endregion

        private bool allowClose = true;

        public frmConnectonWizard()
        {
            InitializeComponent();
        }

        private void frmConnectonWizard_Load(object sender, EventArgs e)
        {
            pnlMySQL.Visible = false;
            pnlSQLServer.Visible = false;
            RepositionWindow();
        }

        private void RepositionWindow()
        {
            panel1.Top = 0;
            panel1.Left = 0;
            pnlMySQL.Top = pnlSQLServer.Top = panel1.Height;
            pnlMySQL.Left = pnlSQLServer.Left = 0;

            panel2.Top = panel1.Height + (pnlMySQL.Visible == true ? pnlMySQL.Height : 0) + (pnlSQLServer.Visible == true ? pnlSQLServer.Height : 0);
            panel2.Left = 0;

            //calculate gutter space
            int space = this.Height - this.ClientRectangle.Height;
            this.Height = space + panel1.Height + panel2.Height + (pnlMySQL.Visible == true ? pnlMySQL.Height : 0) + (pnlSQLServer.Visible == true ? pnlSQLServer.Height : 0);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlMySQL.Visible = false;
            pnlSQLServer.Visible = false;
            if (comboBox1.Text == "MySQL Database")
                pnlMySQL.Visible = true;
            else if (comboBox1.Text == "Microsoft SQL Server")
                pnlSQLServer.Visible = true;
            RepositionWindow();
        }

        private void sqlSqlAuth_CheckedChanged(object sender, EventArgs e)
        {
            sqlUsername.Enabled = sqlSqlAuth.Checked;
            sqlPassword.Enabled = sqlSqlAuth.Checked;
            //btnOK.Enabled = !sqlSqlAuth.Checked;
            EnableOkButton();
        }

        private void mysqlDB_DropDown(object sender, EventArgs e)
        {
            ConnectionBuilder conBuilder = new ConnectionBuilder();
            List<string> databases = conBuilder.MySQLGetDatabases(mysqlServer.Text, mysqlUsername.Text, mysqlPassword.Text);
            mysqlDB.Items.Clear();
            if (databases == null) return;
            foreach (string database in databases)
            {
                mysqlDB.Items.Add(database);
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (TestConnection())
            {
                MessageBox.Show("Connection succeeded", "EPM Workbench");
            }
            else
            {
                MessageBox.Show("Connection failed", "EPM Workbench");
            }
        }

        private bool TestConnection()
        {
            ConnectionBuilder conBuilder = new ConnectionBuilder();
            if (comboBox1.Text == "MySQL Database")
                return conBuilder.MySQLTestConnection(mysqlServer.Text, mysqlUsername.Text, mysqlPassword.Text, mysqlDB.Text);
            else
                return conBuilder.SQLTestConnection(sqlServer.Text, (sqlWinAuth.Checked ? AuthMode.WINDOWS : AuthMode.SQLSERVER), sqlDB.Text, sqlUsername.Text, sqlPassword.Text);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!TestConnection())
            {
                MessageBox.Show("Connection failed" + Environment.NewLine + "Please make sure we have a working database connection.", "EPM Workbench");
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                allowClose = false;
                return;
            }

            ConnectionBuilder conBuilder = new ConnectionBuilder();
            try
            {
                if (comboBox1.Text == "MySQL Database")
                {
                    ConnectionString = conBuilder.MySQLConnectionString(mysqlServer.Text, mysqlUsername.Text, mysqlPassword.Text, mysqlDB.Text);
                    ServerType = "MySQL";
                }
                else
                {
                    if (conBuilder.SQLTestConnection(sqlServer.Text, (sqlWinAuth.Checked ? AuthMode.WINDOWS : AuthMode.SQLSERVER), sqlDB.Text, sqlUsername.Text, sqlPassword.Text))
                    {
                        ConnectionString = conBuilder.SQLConnectionString(sqlServer.Text, (sqlWinAuth.Checked ? AuthMode.WINDOWS : AuthMode.SQLSERVER), sqlDB.Text, sqlUsername.Text, sqlPassword.Text);
                        ServerType = "MSSQL";
                    }
                    else
                    {
                        MessageBox.Show("Connection parameters are not valid");
                    }
                }
                allowClose = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void mysqlServer_TextChanged(object sender, EventArgs e)
        {
            EnableOkButton();
        }

        private void EnableOkButton()
        {
            if (comboBox1.Text == "MySQL Database")
            {
                btnOK.Enabled = mysqlServer.Text != "" && mysqlUsername.Text != "" && mysqlDB.Text != "";
            }
            else
                btnOK.Enabled = sqlServer.Text != "" && (sqlWinAuth.Checked ? true : (sqlUsername.Text != "")) && sqlDB.Text != "";
        }

        private void mysqlUsername_TextChanged(object sender, EventArgs e)
        {
            EnableOkButton();
        }

        private void mysqlDB_TextChanged(object sender, EventArgs e)
        {
            EnableOkButton();
        }

        private void sqlDB_DropDown(object sender, EventArgs e)
        {
            ConnectionBuilder conBuilder = new ConnectionBuilder();
            try
            {
                List<string> databases = conBuilder.SQLGetDatabases(sqlServer.Text, (sqlWinAuth.Checked ? AuthMode.WINDOWS : AuthMode.SQLSERVER), sqlUsername.Text, sqlPassword.Text);
                sqlDB.Items.Clear();
                if (databases == null) return;
                foreach (string database in databases)
                {
                    sqlDB.Items.Add(database);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void sqlServer_TextChanged(object sender, EventArgs e)
        {
            EnableOkButton();
        }

        private void sqlDB_TextChanged(object sender, EventArgs e)
        {
            EnableOkButton();
        }

        private void sqlUsername_TextChanged(object sender, EventArgs e)
        {
            EnableOkButton();
        }

        private void frmConnectonWizard_FormClosing(object sender, FormClosingEventArgs e)
        {
            //MessageBox.Show(e.CloseReason.ToString());
            if (e.CloseReason == CloseReason.None)
            {
                e.Cancel = !allowClose;
            }
            else
            {
                ConnectionString = "";
                ServerType = "";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            allowClose = true;
        }
    }
}
